#include "stdafx.h"
#include "Inventory.h"

extern sf::View gView;

Inventory gInventory;

Item::Item(sf::Sprite& sprite, sf::Vector2f targetPos) : sprite(sprite), targetPos(targetPos),
	finished(false), timer(0.f), startPos(sprite.getPosition())
{}

void Inventory::Add(sf::Sprite& addedItem)
{
	addedItem.setPosition(addedItem.getPosition() - gView.getCenter() + gView.getSize() * .5f);
	items.push_back(Item(addedItem, sf::Vector2f(gView.getSize().x - 16.f  - 32.f * items.size(), 16.f)));
}

void Inventory::Step()
{
	for (int i = 0; i < items.size(); ++i)
	{
		if (!items[i].finished)
		{
			if (items[i].timer >= 1.0f)
			{
				items[i].finished = true;
			}
			else
			{
				float x = items[i].startPos.x * (1.0f - items[i].timer) + items[i].targetPos.x * items[i].timer;
				float scale = 1.f - .5f * items[i].timer;
				float y = items[i].timer <= .5f
					? items[i].startPos.y * (1.0f - sin(items[i].timer * 2.f * PI_2)) + (items[i].startPos.y + 200.f) * sin(items[i].timer * 2.f * PI_2)
					: (items[i].startPos.y + 200.f) * sin((1.f - items[i].timer) * 2.f * PI_2) + items[i].targetPos.y * sin((items[i].timer - .5f) * 2.f * PI_2);
				items[i].sprite.setPosition(x, y);
				items[i].timer += DELTA;
				items[i].sprite.setScale(scale, scale);
			}
		}
	}
}

void Inventory::Draw(sf::RenderWindow& window)
{
	window.setView(sf::View(sf::FloatRect(.0f, .0f, gView.getSize().x, gView.getSize().y)));
	for (int i = 0; i < items.size(); ++i)
		window.draw(items[i].sprite);
}